package com.lxr0860.lxr0860production_test.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.lxr0860.lxr0860production_test.bean.FactoryTestItem;
import com.lxr0860.lxr0860production_test.R;

import java.util.List;

/**
 * Created by 11 on 2018/3/21.
 */

public class FactoryTestIemAdapter extends BaseAdapter {
    private Context context;
    private List<FactoryTestItem> items;
    private OnGridButtonClickListener mClickListener;

    public interface OnGridButtonClickListener {
        void startNextActivity(int position);
    }

    public void setOnGridButtonClickListener(OnGridButtonClickListener listener) {
        this.mClickListener = listener;
    }

    public FactoryTestIemAdapter(Context context, List<FactoryTestItem> items) {
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_test_layout, parent, false);
            holder = new Holder();
            holder.mTvItemTitle = (TextView) convertView.findViewById(R.id.tv_test_item_title);
            holder.mBtnItemIcon = (Button) convertView.findViewById(R.id.btn_test_item_icon);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        holder.mTvItemTitle.setText(items.get(position).getTitle());
        holder.mBtnItemIcon.setBackground(ContextCompat
                .getDrawable(context, items.get(position).getBut_bg_id()));
        if(items.get(position).isCanTest()){
            holder.mBtnItemIcon.getBackground().setAlpha(255);
        }else {
            holder.mBtnItemIcon.getBackground().setAlpha(80);
        }
        holder.mBtnItemIcon.setEnabled(items.get(position).isCanTest());
        holder.mBtnItemIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickListener.startNextActivity(position);
            }
        });
        return convertView;
    }

    class Holder {
        TextView mTvItemTitle;
        Button mBtnItemIcon;
    }
}
